
from openpyxl import load_workbook


class HandleExcel:
    def __init__(self, file, sheet_name):
        self.workbook = load_workbook(file)
        self.sheet = self.workbook[sheet_name]

    def read_data(self):
        all_data=[]
        data=list(self.sheet.values)
        print(data)
        keys=data[0]
        for row in data[1:]:
            value=dict(zip(keys,row))
            all_data.append(value)
        return all_data


    def read_data2(self):
        all_data = []
        keys = [self.sheet.cell(1, i).value for i in range(1, self.sheet.max_column + 1)]

        for row_index in range(2, self.sheet.max_row + 1):

            value = []
            for column_index in range(1, self.sheet.max_column + 1):
                value.append(self.sheet.cell(row_index, column_index).value)

            values = dict(zip(keys, value))

            all_data.append(values)

        return all_data


if __name__ == '__main__':
    file_path = "/Users/jiayunkong/work_space/pythonProject/data/testCase.xlsx"
    HE = HandleExcel(file_path, "invest")
    print(HE.read_data())
